<?php
/**
* mysql
*/
class mysql
{
	private $str;
	private $con;
	/**
	 * 连接数据库
	 * @param  array $config [description]
	 * @return [type]         [description]
	 */
	public function __construct()
	{
		$this->con = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_DBNAME) or die($this->err(mysqli_connect_errno()));
		mysqli_set_charset($this->con, DB_CHARSET);
	}
	/**
	 * 关闭数据库
	 */
	public function __destruct()
	{
		mysqli_close($this->con);
	}
	/**
	 * 报错函数
	 * @param  string $error [description]
	 * @return [type]        [description]
	 */
	function err($error)
	{
		die("对不起，您的操作有误，错误原因为：" . $error);
	}
	/**
	 * 执行sql语句
	 * @param  string $sql [description]
	 * @return bool      返回true、资源或false
	 */
	function query($sql)
	{
		if (!($query = mysqli_query($this->con, $sql))) {
			$this->err($sql . "<br>" . mysqli_error($this->con));
		} else {
			return $query;
		}
	}
	/**
	 * 遍历资源,获取所有数据
	 * @param  source $query [description]
	 * @return array        [description]
	 */
	function findAll($query)
	{
		while ($rows = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
			$this->list[] = $rows;
		}
		return isset($this->list) ? $this->list : array();
	}
	/**
	 * 返回得到一条数据
	 * @param  source $query [description]
	 * @return array        [description]
	 */
	function findOne($query)
	{
		return mysqli_fetch_array($query, MYSQLI_ASSOC);
	}
	/**
	 * 返回指定行的指定字段的值
	 * @param  source  $query [description]
	 * @param  integer $row   [description]
	 * @param  integer $field [description]
	 * @return array         [description]
	 */
	function findResult($query, $row = 0, $field = 0)
	{
		mysqli_data_seek($query, $row);	//函数调整结果指针到结果集中的一个任意行
		$rows = mysqli_fetch_array($query);
		return $rows[$field];
	}
	/**
	 * 记录插入操作
	 * @param  string $table [description]
	 * @param  array $arr   [description]
	 * @return number        [description]
	 */
	function insert($table, $array)
	{
		$keys = "`" . join("`,`", array_keys($array)) . "`";	//join() 函数返回一个由数组元素组合成的字符串。
		$vals = "'" . join("','", array_values($array)) . "'";
		$sql = "INSERT {$table}($keys) VALUES({$vals})";
		mysqli_query($this->con, $sql);
		return mysqli_insert_id($this->con);
	}
	/**
	 * 记录更新操作
	 * @param  string $table [description]
	 * @param  array $array [description]
	 * @param  string $where [description]
	 * @return number        [description]
	 */
	function update($table, $array, $where = null)
	{
		foreach ($array as $key => $val) {
			if ($this->str == null) {
				$sep = "";
			} else {
				$sep = ",";
			}
			$this->str .= $sep . "`" . $key . "`" . "='" . $val . "'";
		}
		$sql = "UPDATE {$table} SET {$this->str} " . ($where == null ? null : " WHERE " . $where);
		$result = mysqli_query($this->con, $sql);
		if ($result) {
			return mysqli_affected_rows($this->con);
		} else {
			return false;
		}
	}
	/**
	 * 删除记录
	 * @param  string $table [description]
	 * @param  string $where [description]
	 * @return number        [description]
	 */
	public function delete($table, $where = null)
	{
		$where = $where == null ? null : " WHERE " . $where;
		$sql = "DELETE FROM {$table} {$where}";
		mysqli_query($this->con, $sql);
		return mysqli_affected_rows($this->con);
	}
}
